﻿using HR.Config;
using HR.Filters;
using HR.IService;
using HR.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections;
using System.IO;

namespace HR.Controllers
{
    public class EngageResumeController : Controller
    {
        //简历登记服务接口
        private readonly IEngageResumeService iers;
        //职位分类接口
        private readonly IConfig_majorKindService icmks;
        //职称接口
        private readonly IJob_setService jss;

        public EngageResumeController(IEngageResumeService iers, IConfig_majorKindService icmks, IJob_setService jss)
        {
            this.iers = iers;
            this.icmks = icmks;
            this.jss = jss;
        }
        //[HttpGet]
        //public IActionResult ToApplyJob(List<int> rmks)
        //{
        //    return RedirectToAction('');
        //}

        /// <summary>
        /// 简历登记视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [TypeFilter(typeof(RoleFilterAttribute))]
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 职位类别和职称下拉列表框数据源
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> FillCombox()
        {
            var list = new List<object>();
            //职位分类集合
            List<config_major_kind> l1 = await icmks.CMKOnPageFill();
            //职称集合
            Paging<config_public_char> l2 = await jss.JobSetFill();
            list.Add(l1);
            list.Add(l2);
            return Json(list);
        }
        [HttpPost]
        public IActionResult uploadImg(IFormCollection files)
        {
            try
            {
                string dd = files["File"];
                var form = files;//定义接收类型的参数
                Hashtable hash = new Hashtable();
                IFormFileCollection cols = Request.Form.Files;
                if (cols == null || cols.Count == 0)
                {
                    return Json("没有上传文件");
                }
                foreach (IFormFile file in cols)
                {
                    //定义图片数组后缀格式
                    string[] LimitPictureType = { ".JPG", ".JPEG", ".GIF", ".PNG", ".BMP" };
                    //获取图片后缀是否存在数组中
                    string currentPictureExtension = Path.GetExtension(file.FileName).ToUpper();
                    if (LimitPictureType.Contains(currentPictureExtension))
                    {
                        //为了查看图片就不在重新生成文件名称了
                        // var new_path = DateTime.Now.ToString("yyyyMMdd")+ file.FileName;
                        var new_path = Path.Combine("uploads/images/", file.FileName);
                        var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", new_path);
                        using (var stream = new FileStream(path, FileMode.Create))
                        {
                            //上传到wwwroot
                        }
                    }
                    else
                    {
                        return Json("请上传指定格式的图片");
                    }
                }
                return Json( "上传成功");
            }
            catch (Exception ex)
            {
                return Json("上传失败" + ex.ToString());

            }

        }
        //public IActionResult imgUploader()
        //{
        //    return 
        //}
        /// <summary>
        /// 简历登记新增
        /// </summary>
        /// <param name="er"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> EngageResumeAdd([FromBody] engage_resume er)
        {
            string? uname = HttpContext.Session.GetString("uname");
            er.register = uname;
            int res = await iers.EngageResumeAdd(er);
            return Json(res);
        }
        /// <summary>
        /// 简历筛选视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [TypeFilter(typeof(RoleFilterAttribute))]
        public IActionResult IndexFilter()
        {
            return View();
        }
        /// <summary>
        /// 职位类型联级选择器
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> CMKCascader()
        {
            List<CMajorKind> list = await icmks.CMKCascader();
            return Json(list);
        }
        /// <summary>
        /// 模糊查询后的简历集合
        /// </summary>
        /// <param name="erf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ERFiltrate([FromBody] EngageResumeFiltrate erf)
        {
            List<engage_resume> list = await iers.ERFiltrate(erf);
            return Json(list);
        }
        /// <summary>
        /// 模糊查询后的有效简历集合
        /// </summary>
        /// <param name="erf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ERFiltrate2([FromBody] EngageResumeFiltrate erf)
        {
            List<engage_resume> list = await iers.ERFiltrate2(erf);
            return Json(list);
        }
        /// <summary>
        /// 筛选简历后的修改操作
        /// </summary>
        /// <param name="er"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> EngageResumeUdp([FromBody] engage_resume er)
        {
            string ? uname = HttpContext.Session.GetString("uname");
            er.register = uname;
            int res = await iers.EngageResumeUdp(er);
            return Json(res);
        }
        /// <summary>
        /// 筛选简历后的删除操作
        /// </summary>
        /// <param name="er"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> EngageResumeDel(short res_id)
        {
            int res = await iers.EngageResumeDel(res_id);
            return Json(res);
        }
        /// <summary>
        /// 有效简历视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [TypeFilter(typeof(RoleFilterAttribute))]
        public IActionResult IndexValid()
        {
            return View();
        }
        public async Task<IActionResult> EngageResumeValidFill(int pgsize,int pgcurrent)
        {
            Paging<engage_resume> pg = await iers.EngageResumeValidFill(pgsize,pgcurrent);
            return Json(pg);
        }
    }
}
